//
//  WKTransferViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/6.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKTransferViewController.h"

@interface WKTransferViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
}

@property (weak, nonatomic) IBOutlet UIButton *receiptAddressBtn;//收款人地址按钮
@property (weak, nonatomic) IBOutlet UITextField *amount;//转账金额
@property (weak, nonatomic) IBOutlet UITextField *remark;//备注
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;//滑动条
@property (weak, nonatomic) IBOutlet UILabel *fee;//旷工费用
@property (weak, nonatomic) IBOutlet UIButton *transferBtn;//转账按钮
@property (nonatomic, strong) UIButton *scanBtn;//扫描按钮
@property (nonatomic, strong) UIButton *addBtn;//添加按钮
@property (nonatomic,strong)UIPickerView *adressPickerView;
@property (nonatomic,strong)NSArray *team;
@property (nonatomic, strong) NSMutableArray *addressArray;//数据源
@end

@implementation WKTransferViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.progressSlider.minimumValue = 0.00001;
    self.progressSlider.maximumValue = 0.001;
    self.progressSlider.value = 0.0005;
    self.fee.text = [NSString stringWithFormat:@"%f",self.progressSlider.value];
    //picker
    self.adressPickerView.delegate=self;
    self.adressPickerView.dataSource=self;
    
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = [NSString stringWithFormat:@"%@转账",self.walletModel.coinType];
    self.navigationView.backgroundColor = [UIColor clearColor];
    [self.navigationView.topTitleView addSubview:self.scanBtn];
    [self.navigationView.topTitleView addSubview:self.addBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    if ([self.walletModel.coinType isEqualToString:@"ETH"]) {
        [self getEthFeesRange];
    }
}

- (void)layoutUI {
//    self.receiptAddressBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    self.transferBtn.backgroundColor = AppManager.themeColor;
    self.transferBtn.titleLabel.font = AppManager.btnFont;
    self.transferBtn.layer.cornerRadius = 5;
    self.transferBtn.layer.masksToBounds = YES;
}

//获取以以太币交易费区间
- (void)getEthFeesRange {
    [httpManager getEthFeesRangeSuccess:^(NSDictionary *result) {
        
        NSDictionary *data = [result objectForKey:@"data"];
        NSString *min = [data objectForKey:@"min"];
        NSString *max = [data objectForKey:@"max"];
        DLog(@"min = %@",min);
        DLog(@"max = %@",max);
        
    } failure:^(NSError *error) {
        
    }];
}

//滑动进度条
- (IBAction)progressSliderChange:(UISlider *)sender {
    self.fee.text = [NSString stringWithFormat:@"%f",sender.value];
}

#pragma mark - Button点击事件
//点击扫描按钮
- (void)clickScanBtn:(UIButton *)sender {
    WKScanCodeViewController *scanCode = [[WKScanCodeViewController alloc] init];
    [self.navigationController pushViewController:scanCode animated:YES];
}

//点击添加按钮
- (void)clickAddBtn:(UIButton *)sender {
    WKAddAddressViewController *addAddress = [[WKAddAddressViewController alloc] init];
    [self.navigationController pushViewController:addAddress animated:YES];
}

//点击收款人地址
- (IBAction)clickReceiptAddressBtn:(id)sender {
    DLog(@"hahahah");
    [self.view addSubview:_adressPickerView];
    [httpManager getAddressListWithPageIndex:1 pageSize:10 success:^(NSDictionary *result) {
        
        NSDictionary *data = [result objectForKey:@"data"];
        for (NSDictionary *dict in data) {
            WKAddressListModel *list = [WKAddressListModel mj_objectWithKeyValues:dict];
            [self.addressArray addObject:list];
        }
        DLog(@"%@",result);
        //[self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

//点击转账按钮
- (IBAction)clickTransferBtn:(id)sender {
    
//    //比特币
//    [httpManager buildTransactionWithWalletId:self.walletModel.id toAddress:@"moSJChr3z4vRU3FrGFoVCGjGvxUmCmAiC1" amount:self.amount.text fee:@"10000" success:^(NSDictionary *result) {
//
//        DLog(@"----------------------第二次网络请求----------------------");
//        NSDictionary *data = [result objectForKey:@"data"];
//        NSString *txHex = [WKBlockChainManager buildBtcTransactionWithPrivate:@"cUPgytyiktxGhvQSwhwYTuKzPpam7kDLnoot4wSNYF4eDpUwE5db" dict:data];
//        [httpManager addTransactionWithCoinType:self.walletModel.coinType walletId:self.walletModel.id toAddress:@"moSJChr3z4vRU3FrGFoVCGjGvxUmCmAiC1" remark:self.remark.text txHex:txHex success:^(NSDictionary *result) {
//
//        } failure:^(NSError *error) {
//
//        }];
//
//    } failure:^(NSError *error) {
//
//    }];
    
    
    //以太币
    [httpManager buildTransactionWithWalletId:self.walletModel.id toAddress:@"0x4b9d091a701bc54925099a918b20238c523dc0f6" amount:self.amount.text fee:@"25200000000000" success:^(NSDictionary *result) {

        DLog(@"----------------------第二次网络请求----------------------");
        NSDictionary *data = [result objectForKey:@"data"];
        NSString *txHex = [WKBlockChainManager buildEthTransactionWithPrivate:@"98254a992ec6df2fb0fd4a45f511a22ddf79f86328327b97507db5e6f9dc8ff4" dict:data];
        [httpManager addTransactionWithCoinType:self.walletModel.coinType walletId:self.walletModel.id toAddress:@"0x4b9d091a701bc54925099a918b20238c523dc0f6" remark:self.remark.text txHex:txHex success:^(NSDictionary *result) {

        } failure:^(NSError *error) {

        }];

    } failure:^(NSError *error) {

    }];
}

#pragma mark - 懒加载
- (UIButton *)scanBtn {
    if (_scanBtn == nil) {
        _scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _scanBtn.frame = CGRectMake(AppManager.width-self.navigationView.buttonWidth/2, 0, self.navigationView.buttonWidth/2, self.navigationView.topTitleViewHeight);
        [_scanBtn setImage:[UIImage imageNamed:@"saoma"] forState:UIControlStateNormal];
        [_scanBtn addTarget:self action:@selector(clickScanBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}

- (UIButton *)addBtn {
    if (_addBtn == nil) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(AppManager.width-self.navigationView.buttonWidth, 0, self.navigationView.buttonWidth/2, self.navigationView.topTitleViewHeight);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_addBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(clickAddBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1; // 返回1表明该控件只包含1列
}

//UIPickerViewDataSource中定义的方法，该方法的返回值决定该控件指定列包含多少个列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // 由于该控件只包含一列，因此无须理会列序号参数component
    // 该方法返回teams.count，表明teams包含多少个元素，该控件就包含多少行
    return 1;
}


// UIPickerViewDelegate中定义的方法，该方法返回的NSString将作为UIPickerView
// 中指定列和列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 由于该控件只包含一列，因此无须理会列序号参数component
    // 该方法根据row参数返回teams中的元素，row参数代表列表项的编号，
    // 因此该方法表示第几个列表项，就使用teams中的第几个元素
    
    return [_team objectAtIndex:row];
}

// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    // 使用一个UIAlertView来显示用户选中的列表项
    UIAlertView* alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:[NSString stringWithFormat:@"你选中的球队是：%@"
                                   , [ _team objectAtIndex:row]]
                          delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil];
    [alert show];
}
@end
